package com.weng.coder.controller;

import com.weng.coder.common.utils.Constants;
import com.weng.coder.entity.ProjectUser;
import com.weng.coder.service.ProjectUserService;
import com.weng.framework.core.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 项目用户 Web API
 *
  * @author wengzhonghui
 * @创建时间 2018-07-17 17:22:12
 *
 */
@RestController
@RequestMapping("/projectUser") 
@Api(value = "项目用户相关接口")
public class ProjectUserController{
	@Resource
	private ProjectUserService projectUserService;
	
	

	
	 /**
	 * 获取项目用户
	 * 
	 */
	@RequestMapping(value="get-one",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取项目用户", notes = "获取项目用户")
	public ResponseData getProjectUser(String id, Model model
			,HttpServletRequest request) throws Exception{
		if(StringUtils.isEmpty(id)){
			return ResponseData.failed("项目用户Id不能为空！");
		}
		
		ProjectUser projectUser = this.projectUserService.getOne(id);
		return ResponseData.success(projectUser);
	}
	 
	/**
	 * 添加项目用户
	 * 
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "添加项目用户", notes = "添加项目用户")
	public ResponseData addProjectUser(ProjectUser projectUser, Model model
			,HttpServletRequest request) throws Exception{
		if(projectUser==null ){
			return ResponseData.failed("项目用户不能为空！");
		}
		projectUserService.save(projectUser);
		request.setAttribute(Constants.AOP_LOG_DESCRIPTION, "添加项目用户，【"+projectUser.getId()+"】");
		return ResponseData.success("添加项目用户成功！");
	}
	
	/**
	 * 更新项目用户
	 */
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新项目用户", notes = "更新项目用户")
	public ResponseData updateProjectUser(ProjectUser projectUser,HttpServletRequest request) throws Exception{
		if(projectUser==null ){
			return ResponseData.failed("项目用户不能为空！");
		}
		projectUserService.save(projectUser);
		request.setAttribute(Constants.AOP_LOG_DESCRIPTION, "更新项目用户，账号【"+projectUser.getId()+"】");
		return ResponseData.success("更新成功！");
	}
	
	
	/**
	 * 删除项目用户
	 */
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "删除项目用户", notes = "删除项目用户,多个ID以,号分隔")
	@Transactional
	public  ResponseData deleteprojectUser( String ids
			,HttpServletRequest request) throws Exception{
		String[] projectUserIdList = ids.split(",");
		if(projectUserIdList == null || projectUserIdList.length < 1){
			return ResponseData.failed("项目用户ID不能为空");
		}
		StringBuilder logDesc = new StringBuilder();
		List<ProjectUser> projectUserList = projectUserService.findByIds(Arrays.asList(projectUserIdList));
		if(projectUserList!=null && projectUserList.size()>0){
			for(ProjectUser projectUser:projectUserList){
				if(logDesc.length()>0){
					logDesc.append(",");
				}
				logDesc.append(projectUser.getId());
				this.projectUserService.delete(projectUser);
			}
			request.setAttribute(Constants.AOP_LOG_DESCRIPTION, "删除项目用户【"+logDesc.toString()+"】");
		}
		return ResponseData.success("删除成功");
	}


}

